package com.onlyxiahui.framework.action.dispatcher.extend.impl;

import java.util.HashMap;
import java.util.Map;

import com.onlyxiahui.framework.action.dispatcher.extend.ActionBox;
import com.onlyxiahui.framework.action.dispatcher.factory.ObjectFactory;

/**
 * Date 2019-01-12 10:10:56<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */

public class DefaultActionBox implements ActionBox {

	Map<String, Class<?>> classMap = new HashMap<>();
	ObjectFactory objectFactory = new ObjectFactory();

	@Override
	public Object getAction(Class<?> type) {
		return objectFactory.getObject(type);
	}

	@Override
	public Object getAction(String className) {
		Class<?> type = null;
		if (classMap.containsKey(className)) {
			type = classMap.get(className);
		} else {
			try {
				type = Class.forName(className);
				classMap.put(className, type);
			} catch (Exception e) {
				classMap.put(className, null);
			}
		}
		if (null == type) {
			return null;
		} else {
			return getAction(type);
		}
	}
}
